package com.api.smoke.wall;

import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.IOrganizationApiService;
import com.zyncro.api.IUserApiService;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.CollectionResultApi;
import com.zyncro.api.model.EventApi;
import com.zyncro.api.model.ObjectResultApi;
import com.zyncro.api.model.UserFollowApiType;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetActivityEventsSmoke extends AbstractApiTest {

	private CollectionResultApi<EventApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		final DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		final String followerName = getRepo().getPropertyDto(domainName + "-Follower", new DtoFactory<String>() {

			public String createDto() {

				try {
					String followed = getApi().getService(IOrganizationApiService.class).addUser("nameFollower", "lastname",
							"userToFollow@yopmail.com", "123456", 1000, true, true, true, true, null, null);

					getApi().getService(IUserApiService.class).followUser(followed, UserFollowApiType.ACTIVITY);

					return followed;

				} catch (Exception e) {
					throw new IllegalStateException("Exception creating test data", e);
				}
			}
		});

		String fileName = getRepo().getPropertyDto(domainName + "-Events", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");
				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {

					ObjectResultApi<String> createGroupResult = getApi().getService(IGroupApiService.class).createGroup("name",
							"description", true, false, true, true, true, true, false, null);
					String groupUrn = createGroupResult.getObject();

					getApi().login(followerName, "123456");

					getApi().getService(IGroupApiService.class).joinGroup(groupUrn);

					String eventUrn = getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo", null, null,
							null, null, null, null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo 2", null, null, null, null, null,
							null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "mensaje de grupo 3", null, null, null, null, null,
							null, false, false);

					getApi().getService(IWallApiService.class).publishInGroup(groupUrn, "commentario de grupo", null, null, null, eventUrn,
							null, null, false, false);

					getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

					CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getActivityEvents(null, 0, false,
							false, null);

					DataUtil.saveDtoToFile(result, fileName);
				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<CollectionResultApi<EventApi>>() {
		});
	}

	@Test
	public void testGetPersonalFeedEvents() throws ZyncroApiException {

		CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getActivityEvents(null, 0, false, false, null);

		AssertUtil.assertDTOEquals(result.getElements(), expectedResults.getElements());
	}
}
